﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;

namespace lottery
{
    class CheckCode
    {

        string iniFile;
        public string corpName
        {
            get
            {
                
                return this.get("CorpName");
            }
        }

        public string SnCode {
            get {
                //return Convert.ToString(Encoding.Default.GetBytes(this.get("Sn")));
                return this.get("Sn");
            }
        }
        public CheckCode()
        {
            iniFile = Application.StartupPath + @"\license.lic";
            Initialize();
        }
        public void Initialize()
        {
            if (!File.Exists(iniFile))
            {
                string defaultIni = @"
CorpName=
Sn=
";
                File.WriteAllText(iniFile, defaultIni, Encoding.Default);
            }
            
        }
        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public string get(string name)
        {

            Initialize();
            StreamReader sr = new StreamReader(iniFile, Encoding.Default); // 使用默认编码


            while (!sr.EndOfStream)//判断是否读完文件，EndOfStream表示是否是流文件的结尾
            {
                string str = sr.ReadLine();
                if (str != "" && str.Trim().Substring(0, 1) != "#" && str.Split('=').Length > 1 && str.Split('=')[0] == name)
                {
                    sr.Close();
                    sr.Dispose();
                    return str.Split('=')[1];
                }

                // 按照行读取
            }
            sr.Close();
            sr.Dispose();
            return "";
        }

        public Boolean checkCode()
        {


            if (corpName == "" || SnCode == "")
            {
                return false;
            }

            if (check(corpName, SnCode))
            {
                return true;
            }

            return false;
        }

        static public Boolean check(string corpNameA, string SnCodeA)
        {
            if (corpNameA.Trim() == "" || SnCodeA.Trim() == "")
            {
                return false;
            }

            int corpNameLength = corpNameA.Length;
            int SnCodeLength = SnCodeA.Length;

            if (SnCodeLength < 10)
            {
                return false;
            }

            string corpMd5 = Md5(Md5(Md5(Md5(Md5(corpNameA)))));


            if (SnCodeA == corpMd5)
            {
                return true;
            }
            return false;
        }


        static public string Md5(string str)
        {
            string cl = str;
            string pwd = "";
            MD5 md5 = MD5.Create();//实例化一个md5对像
            // 加密后是一个字节类型的数组，这里要注意编码UTF8/Unicode等的选择　
            byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
            // 通过使用循环，将字节类型的数组转换为字符串，此字符串是常规字符格式化所得
            for (int i = 0; i < s.Length; i++)
            {
                //将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母，如果使用大写（X）则格式后的字符是大写字符
                pwd = pwd + s[i].ToString("X");

            }
            return pwd;
        }
    }
}
